package com.halflife.framework.converter;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.function.Consumer;

/**
 * Created by zhaoyf on 2018/8/30.
 */
public class MapHandler {
    public static Map handleMap(Map map) {
        if (map == null) {
            map = new HashMap();
        }
        if (map.size() > 0) {
           Iterator<String> its = map.keySet().iterator();
            while (its.hasNext()) {
                String key = its.next();
                if (map.get(key).equals("")) {
                    its.remove();
                }
            }
        }
        return map;
    }

    public static String escapeChar(String sql) {
        return sql.replaceAll("'", "\\\\'");
    }

    public static void main(String[] args) {
        System.out.println("asdf'asdf'asfd'd".replaceAll("'", "\\\\'"));
    }
    public static Map<String,Object> handleParamMap(Map map, StringBuilder sb, Consumer<String> consumer) {
        Map<String, Object> sqlQueryMap = new HashMap<>();
        sb.append(" 1=1 ");
        if (map!=null && map.size() > 0) {
           Iterator<String> its = map.keySet().iterator();
            while (its.hasNext()) {
                String key = its.next();
                Object value = map.get(key);
                if (key.equals("_")) {
                    continue;
                }
                if (value.equals("")) {
                    continue;
                }
                if(key.contains("keyword")){
                    consumer.accept((String)value);
                    continue;
                }
                if (key.contains("_span_start")) {
                    String real_name = key.replaceAll("\\_span\\_start*","");
                    System.out.println(real_name);
                    sb.append(" and date_format(t.").append(real_name).append(",'%Y-%m-%d')");
                    sb.append(">='");
                    sb.append(value);
                    sb.append("'");

                }
                else if (key.contains("_span_end")) {
                    String real_name = key.replaceAll("\\_span\\_end*","");
                    System.out.println(real_name);
                    sb.append(" and date_format(t.").append(real_name).append(",'%Y-%m-%d')");
                    sb.append("<='");
                    sb.append(value);
                    sb.append("'");

                }
                else if(key.contains("_mcss")){
                    String real_name = key.replaceAll("\\_mcss*","");
                    sb.append(" and (");
                    String[] values = ((String)value).split("\\$\\$");
                    StringBuilder sub = new StringBuilder();
                    boolean is = false;
                    for (String v : values) {
                        if (is) {
                            sb.append(" or ");
                        }
                        sb.append("  t.").append(real_name).append(" like '%").append(escapeChar(v)).append("%'");
                        is =true;
                    }
                    sb.append(")");
                }
                else if(key.contains("_mss")){
                    String real_name = key.replaceAll("\\_mss*","");
                    sb.append(" and (");
                    String[] values = ((String)value).split(",");
                    StringBuilder sub = new StringBuilder();
                    boolean is = false;
                    for (String v : values) {
                        if (is) {
                            sb.append(" or ");
                        }
                        sb.append("  t.").append(real_name).append(" like '%").append(escapeChar(v)).append("%'");
                        is =true;
                    }
                    sb.append(")");
                }
                else if(key.contains("_ss")){
                    String real_name = key.replaceAll("\\_ss*","");
                    sb.append(" and t.").append(real_name);
                    String[] values = ((String)value).split(",");
                    StringBuilder sub = new StringBuilder();
                    for (String v : values) {
                        if (sub.length() > 0) {
                            sub.append(",");
                        }
                        sub.append("'").append(escapeChar(v)).append("'");
                    }
                    if (values.length == 1) {
                        sb.append(" = ").append(sub.toString());
                    } else {
                        sb.append(" in (").append(sub.toString()).append(")");
                    }
                }else{
                    sqlQueryMap.put(key, value);
                }


            }
        }
        return sqlQueryMap;
    }

}
